//
//  LoginHandle.m
//  JYFarm
//
//  Created by 花花 on 2017/10/26.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "LoginHandle.h"
#import "UserDao.h"
#import "HTTPRequest.h"


#define loginToken @"loginToken"
#define loginUserId @"loginUserId"
#define kmerchantName @"kmerchantName"
#define kfreeCash @"kfreeCash"


@implementation LoginHandle
@synthesize userInfor = _userInfor;

static LoginHandle * instance = nil;
static NSString *const kYISLoginAccount =  @"kYISLoginAccount";
+ (instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        instance = [[self alloc]init];
    });
    
    return instance;
}

- (JYUserModel*)userInfor
{
    //不是登录状态 直接返回空值
    if (!self.isLoginStatus)
    {
        return nil;
    }
    else
    {
        if (!_userInfor)
        {
            _userInfor = [UserDao getUser];
        }
    }
    return _userInfor;
}

- (void)setUserInfor:(JYUserModel *)userInfor
{
    if (!userInfor) return;
    
    if (_userInfor!=userInfor)
    {
        _userInfor = userInfor;
    }
}

- (BOOL)isLoginStatus
{
    return [[NSUserDefaults standardUserDefaults] boolForKey:kLoginStatus];
}

- (void)setIsLoginStatus:(BOOL)isLoginStatus
{
    NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];

    [userdefault setBool:isLoginStatus forKey:kLoginStatus];
    [userdefault synchronize];
}

- (void)setToken:(NSString *)token{
    NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];
    
    [userdefault  setValue:token forKey:loginToken];
    [userdefault synchronize];
    
    
}
- (NSString *)token{
    
    return [[NSUserDefaults standardUserDefaults]valueForKey:loginToken];
}

- (void)setUserId:(NSString *)userId{
    
    NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];
    
    [userdefault  setValue:userId forKey:loginUserId];
    [userdefault synchronize];
    
}

- (NSString *)userId{
    
    return [[NSUserDefaults standardUserDefaults]valueForKey:loginUserId];
    
}




- (void)saveUserInfoToLocal:(JYUserModel *)user
{
    [UserDao addUser:user];
}

- (void)loginOutDataHandle
{
    
    self.isLoginStatus = NO;
    
    
     //清空用户信息表
     [UserDao cleanUserInfoTable];
    
     _userInfor = nil;
     //发送退出登录的通知
     [[NSNotificationCenter defaultCenter]postNotificationName:kLogoutNotification object:nil];
     //清空token
    [self setToken:@""];
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    [userDefaults removeObjectForKey:kAccount];
    [userDefaults removeObjectForKey:kPassword];
    [userDefaults removeObjectForKey:kFreeCash];
    [userDefaults removeObjectForKey:kMerchantName];
    [userDefaults removeObjectForKey:kLoginStatus];
    [userDefaults removeObjectForKey:kLoginToken];
    [userDefaults synchronize];
}





- (void)loginSuccessDataHandle
{
    self.isLoginStatus = YES;
    //发送登录成功的通知
    [[NSNotificationCenter defaultCenter] postNotificationName:kLoginSuccessNotification object:nil];
}


- (void)updateLocatUserInfo:(NSDictionary *)keyAndValues
{
    //不是字典不往下执行
    if (![keyAndValues isKindOfClass:[NSDictionary class]]||!keyAndValues)
    {
        return;
    }
    NSArray*keys = keyAndValues.allKeys;
    
    for (NSString*key in keys)
    {
        id value = [keyAndValues objectForKey:key];
        
        [_userInfor setValue:value forKey:key];
    }
    
    [self saveUserInfoToLocal:_userInfor];
}


@end
